<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="floorName">
      <el-input v-model="dataForm.floorName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="规则关闭按钮" prop="imageUrl1">
      <el-upload
        class="avatar-uploader"
        :action="this.$http.adornUrl(`/share/uploadWeChat?token=${this.$cookie.get('token')}`)"
        :show-file-list="false"
        :on-success="handleAvatarSuccess1"
        :before-upload="beforeAvatarUpload">
        <img v-if="this.dataForm.imageUrl1" :src="'http://image.yilianbaihui.cn/consumer/wechat/'+ this.dataForm.imageUrl1" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="规则背景图片" prop="imageUrl2">
      <el-upload
        class="avatar-uploader"
        :action="this.$http.adornUrl(`/share/uploadWeChat?token=${this.$cookie.get('token')}`)"
        :show-file-list="false"
        :on-success="handleAvatarSuccess2"
        :before-upload="beforeAvatarUpload">
        <img v-if="this.dataForm.imageUrl2" :src="'http://image.yilianbaihui.cn/consumer/wechat/'+ this.dataForm.imageUrl2" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="分享规则图片" prop="imageUrl3">
      <el-upload
        class="avatar-uploader"
        :action="this.$http.adornUrl(`/share/uploadWeChat?token=${this.$cookie.get('token')}`)"
        :show-file-list="false"
        :on-success="handleAvatarSuccess3"
        :before-upload="beforeAvatarUpload">
        <img v-if="this.dataForm.imageUrl3" :src="'http://image.yilianbaihui.cn/consumer/wechat/'+ this.dataForm.imageUrl3" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        dataForm: {
          id: 0,
          floorName: '',
          imageUrl1: '',
          imageUrl2: '',
          imageUrl3: ''
        },
        dataRule: {
          floorName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          imageUrl1: [
            { required: true, message: '规则关闭按钮不能为空', trigger: 'blur' }
          ],
          imageUrl2: [
            { required: true, message: '规则背景图片不能为空', trigger: 'blur' }
          ],
          imageUrl3: [
            { required: true, message: '分享规则图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg' || 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 1

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 1MB!')
        }
        return isJPG && isLt2M
      },
      handleAvatarSuccess1 (res, file) {
        this.dataForm.imageUrl1 = res.data
        console.log(this.dataForm.imageUrl1)
      },
      handleAvatarSuccess2 (res, file) {
        this.dataForm.imageUrl2 = res.data
      },
      handleAvatarSuccess3 (res, file) {
        this.dataForm.imageUrl3 = res.data
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/share/getShareRuleById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.floorName = data.data.floorName
                if (data.data.imgName !== '') {
                  this.dataForm.imageUrl1 = data.data.imgName.split(',')[0]
                  this.dataForm.imageUrl2 = data.data.imgName.split(',')[1]
                  this.dataForm.imageUrl3 = data.data.imgName.split(',')[2]
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.imageUrl1 === '' || this.dataForm.imageUrl1.length <= 5) {
          this.$message.error('请上传规则关闭按钮图片！')
          return
        }
        if (this.dataForm.imageUrl2 === '' || this.dataForm.imageUrl2.length <= 5) {
          this.$message.error('请上传规则背景图片！')
          return
        }
        if (this.dataForm.imageUrl3 === '' || this.dataForm.imageUrl3.length <= 5) {
          this.$message.error('请上传分享规则图片！')
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/share/${!this.dataForm.id ? 'addShareRule' : 'modifyShareRuleById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorName': this.dataForm.floorName,
                'imgPath': 'wechat',
                'imgName': this.dataForm.imageUrl1 + ',' + this.dataForm.imageUrl2 + ',' + this.dataForm.imageUrl3
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
